import * as api from '../api/api';
import request from '../utils/request';
import { pageSize } from '../utils/common';

export default {
    namespace: 'order',
    state: {
        storeId: 1,
        stores: { items: [] },
        order: {},
        orders: { totalCount: 0, items: [] },
        orderSearchs: { beginTime: '', endTime: '', orderNo: '', receiverPhone: '', shippingNo: '', payType: '', status: '', pageIndex: 1, pageSize: pageSize, },
        treeorders: { totalCount: 0, items: [] },
        treeorderSearchs:{beginTime: '', endTime: '', orderNo: '', receiverPhone: '', shippingNo: '', payType: '', status: '', pageIndex: 1, pageSize: pageSize },
        chargeOrders: { totalCount: 0, items: [] },
        chargeOrderSearchs: { beginTime: '', endTime: '', orderNo: '', payType: '', keywords: '', orderType: '', pageIndex: 1, pageSize: pageSize, },
        chargeWallets: { totalCount: 0, items: [] },
        chargeWalletSearchs: { beginTime: '', endTime: '', keywords: '', pageIndex: 1, pageSize: pageSize, }
    },
    effects: {
         /**
         * 获取店铺列表
         * @param param0 
         * @param param1 
         */
        *getStores({ payload }, { call, put }) {
            const { callback } = payload;
            const stores: api.AbpApplicationServicesDtoListResultDtoSingleUserMallStoresVOStoreVO =
                yield request(api.ManagestoreManageApiFetchParamCreactor.managestoreManageGetStoresAsync({}))
                const storeId = stores.items&&stores.items[0].id;
                yield put({ type: 'save', payload: { stores } });
                yield put({ type: 'save', payload: { storeId } });
            callback && callback(true);
        },
        /**
         * 获取order列表
         * @param param0 
         * @param param1 
         */
        *getOrders({ payload }, { call, put }) {
            const { beginTime, endTime, orderNo, receiverPhone, shippingNo, status, payType, pageIndex, pageSize, storeId} = payload;
            if(storeId == "请选择")
            {
                return;
            } 
            const formBeginTime = beginTime,
                formEndTime = endTime,
                formOrderNo = orderNo,
                formReceiverPhone = receiverPhone,
                formShippingNo = shippingNo,
                formStatus = status,
                formPayType = payType,
                formStoreId = storeId,
                formMaxResultCount = pageSize,
                formSkipCount = (pageIndex - 1) * pageSize;
            const orders =
                yield request(api.ManageorderManageApiFetchParamCreactor.manageorderManageGetOrdersAsync(
                    { formBeginTime, formEndTime, formOrderNo, formReceiverPhone, formShippingNo, formStatus, formPayType, formStoreId, formMaxResultCount, formSkipCount }
                ))
            yield put({ type: 'save', payload: { orders, orderSearchs: { beginTime, endTime, orderNo, receiverPhone, shippingNo, status, payType, pageIndex, pageSize, } } });
        },
         /**
         * 获取理财产品订单列表
         * @param param0 
         * @param param1 
         */
        *getTreeOrders({ payload }, { call, put }) {
            const { beginTime, endTime, orderNo, receiverPhone, shippingNo, status, payType, pageIndex, pageSize, storeId} = payload;
            if(storeId ==  "请选择")
            {
                return;
            } 
            const formBeginTime = beginTime,
                formEndTime = endTime,
                formOrderNo = orderNo,
                formReceiverPhone = receiverPhone,
                formShippingNo = shippingNo,
                formStatus = status,
                formPayType = payType,
                formStoreId = storeId,
                formMaxResultCount = pageSize,
                formSkipCount = (pageIndex - 1) * pageSize;
            const treeorders =
                yield request(api.ManageorderManageApiFetchParamCreactor.manageorderManageGetTreeOrdersAsync(
                    { formBeginTime, formEndTime, formOrderNo, formReceiverPhone, formShippingNo, formStatus, formPayType, formStoreId, formMaxResultCount, formSkipCount }
                ))
            yield put({ type: 'save', payload: { treeorders, treeorderSearchs: { beginTime, endTime, orderNo, receiverPhone, shippingNo, status, payType, pageIndex, pageSize, } } });
        },
        
        /**
         * 获取order详情
         * @param param0 
         * @param param1 
         */
        *getOrder({ payload }, { call, put }) {
            const { id } = payload;
            const formId = id;
            const order =
                yield request(api.ManageorderManageApiFetchParamCreactor.manageorderManageGetOrderAsync({
                    formId,
                }))
            yield put({ type: 'save', payload: { order } });
        },
        /**
         * 设置发货
         * @param param0 
         * @param param1 
         */
        *setOrderSent({ payload }, { call, put }) {
            const { form, callback } = payload;
            yield request(api.ManageorderManageApiFetchParamCreactor.manageorderManageSetOrderSentAsync(
                { form }
            ));
            callback && callback(true);
        },
        /**
         * 设置已收货
         * @param param0 
         * @param param1 
         */
        *setOrderReceived({ payload }, { call, put }) {
            const { form, callback } = payload;
            yield request(api.ManageorderManageApiFetchParamCreactor.manageorderManageSetOrderReceivedAsync(
                { form }
            ));
            callback && callback(true);
        },
    },
    reducers: {
        save(state, action) {
            return { ...state, ...action.payload };
        },
    },
    subscriptions: {
        setup({ dispatch, history }) {
        },
    },
};